import { LoginRequestData, RegisterRequestData, EmailCodeVerifyRequestData } from './types'
import { getApi, postApi, deleteApi } from '../request'

// 登陆验证
export async function authLogin(data: LoginRequestData) {

    try {
        const res = await postApi("auth/login",
            {
                "captcha": data.captcha,
                "username": data.username,
                "password": data.password
            })
        return res;

    } catch (error) {
        console.error(error); // 处理错误
        
    }



}

// 注册
export async function authRegister(data: RegisterRequestData) {
    if (data.emailCode.length < 6 || data.password.length < 8 || data.username.length < 4) { 
        console.log("提交数据不符合要求！！！");
         return "data-0000"; 
        }

    try {
        const res = await postApi("auth/register",
            {
                "emailCode": data.emailCode,
                "username": data.username,
                "password": data.password
            })

        return res;

    } catch (error) {
        console.error(error); // 处理错误
        return "error-0000"
    }
}

// 获取图片验证码
export async function authCaptcha() {
    try {
        const res = await getApi("auth/captcha")
        return res;

    } catch (error) {
        console.error(error); // 处理错误
        return "error-0000"
    }
}

// 退出登陆
export async function authLogout() {
    try {
        const res = await deleteApi("auth/logout")
        return res;
    } catch (error) {
        console.error(error); // 处理错误
        return "error-0000"
    }
}

//获取邮箱验证码
export async function authEmailCode(data: EmailCodeVerifyRequestData) {
    if (data.email.length < 6 || data.captcha.length < 4) { console.log("提交数据不符合要求！！！"); return "data-0000"; }
    console.log("data")
    console.log(data)
    try {
        const res = await postApi("auth/emailCode", {
            "email": data.email,
            "captcha": data.captcha
        })
     
        return res;
    } catch (error) {
        console.error(error); // 处理错误
        return "error-0000"
    }
}
